<template>
<!-- 登录 -->
<div class="register">
	<form id="ajaxForm" class="login-wrap" action="http://localhost:3000/login/register" method="POST">
		<ul>
			<li>
				<label for="">QQ邮箱 :</label>
				<input type="text" name="username" class="name" v-model="username">
			</li>
			<li>
				<label for="">设置密码 :</label>
				<input type="password" name="password" class="pass" v-model="password">
			</li>
			<li>
				<label for="">确认密码 :</label>
				<input type="password" name="" class="repass" v-model="password1">
			</li>
			<li>
				<label for="">笔名 :</label>
				<input type="text" name="author" class="author" v-model="author">
			</li>
			<li>
				<label for="">邮箱验证 :</label>
				<input type="text" name="" class="code" v-model="yzm">
				<input type="button" value="获取验证码" class="verify" @click="getyzm" v-if="isS">
				<input type="button" disabled="isDisable" :value="num==60?'获取验证码':countDown" class="verify"  v-else>
			</li>
			<li>
				<label for=""></label>
				<input type="button" class="submit " value="立即注册" @click="putRegister">
			</li>
		</ul>
	</form>
</div>
</template>

<script>
export default {
	data(){
		return {
			isS:true,
			username:'',
			password:'',
			password1:'',
			author:'',
			yzm:'',
			number:60,
			isDisable:false
		}
	},
	computed:{
		countDown(){
			return this.number+"秒后可重新发送"
		},
	},
   methods:{
	   //延时函数
	 setTime(obj) {
    if (this.number == 0) {
		this.number=60
        return 
    } else { 
		this.number--;
        // console.log(this.number)
    }
    setTimeout(()=>{ this.setTime(obj) },1000) //每1000毫秒执行一次
},

	   //获取验证码
      async getyzm(){
        let data=await this.axios.get(`login/postmail?email=${this.username}`)
		
		console.log(data)
		this.isS=false
		this.isDisable=true	
		this.setTime(this.number)
	   },

	   
	   //提交注册
		


	  async putRegister(){

		  if(this.username.trim()&&this.password.trim()&&this.yzm.trim()&&this.author.trim()){
			  	let data=await this.axios.post(`login/register`,{
			  username:this.username,
			  password:this.password,
			  yzm:this.yzm,
			  author:this.author,
		  })
		//   console.log(data.data.num) 
		this.isS=true
		this.isDisable=false
		if(data.data.num==1){
			return this.$message.error("账号已经存在了")
		}else if(data.data.num===2){
			 this.$message.success("注册成功，请登录")
			 this.$router.push("/login")
			 return 
		}else if(data.data.num===3){
			this.$message.error("注册失败，请重试")
		}
		  }else{
		  return this.$message.error("请正确填写全部信息") 
	   }
		  	
	   }
	   
   } 


}
</script>

<style scoped>

@charset "utf-8";
body {
	margin: 0;
	padding: 0;
	background-color: #F7F7F7;
	overflow:hidden;
}

ul {
	margin: 0;
	padding: 50px;
	list-style: none;
}

.register {
	position: fixed;
	width: 800px;
	background-color: #FFF;
	border: 1px solid #CCC;
	border-radius: 5px;
	position:fixed;
	left:50%;
	top:50%;
	transform:translate(-50%,-50%);
	z-index:1000;     
	opacity: 0.8; 
}

li {
	display: flex;
	margin: 20px 0;
}

label, input {
	display: block;
	float: left;
	height: 46px;
	font-size: 24px;
	box-sizing: border-box;
	color: #333;
}

label {
	width: 200px;
	line-height: 46px;
	margin-right: 30px;
	text-align: right;
}

input {
	width: 320px;
	padding: 8px;
	line-height: 1;
	outline: none;
	position: relative;
}

input.code {
	width: 120px;
}

input.verify {
	width: 190px;
	margin-left: 10px;
	font-size: 16px;
}

input.disabled {
	background-color: #CCC !important;
}

input[type=button] {
	border: none;
	color: #FFF;
	background-color: #E64145;
	border-radius: 4px;
	cursor: pointer;
}
.avatar {
position: fixed;
margin-top: -20px;    
position: absolute;
top: -60px;
left: 50%;

overflow: hidden;

width: 130px;
height: 130px;
margin-left: -65px;

border: 10px solid #fff;
border-radius: 50%;
box-shadow: 0 1px 5px #ccc;
}
</style>